<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content=
"HTML Tidy for Linux/x86 (vers 1st November 2003), see www.w3.org" />
<meta http-equiv="Content-Type" content=
"text/html; charset=us-ascii" />
<title>5.&nbsp;Common programming pitfalls</title>
<link rel="stylesheet" href="faq.css" type="text/css" />
<meta name="generator" content="DocBook XSL Stylesheets V1.64.1" />
<link rel="home" href="index.html" title=
"Frequently Asked Questions for comp.lang.lisp" />
<link rel="up" href="index.html" title=
"Frequently Asked Questions for comp.lang.lisp" />
<link rel="previous" href="programming.html" title=
"4.&nbsp;Programming questions" />
<link rel="next" href="myths.html" title=
"6.&nbsp;Debunking myths" />
<link rel="article" href="index.html" title=
"Frequently Asked Questions for comp.lang.lisp" />
<!-- generated HTML; do not edit -->
</head>
<body>
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr>
<th colspan="3" align="center">5.&nbsp;Common programming
pitfalls</th>
</tr>
<tr>
<td width="20%" align="left"><a accesskey="p" href=
"programming.html">Prev</a>&nbsp;</td>
<th width="60%" align="center">&nbsp;</th>
<td width="20%" align="right">&nbsp;<a accesskey="n" href=
"myths.html">Next</a></td>
</tr>
</table>
<hr /></div>
<div class="sect1" lang="en" xml:lang="en">
<div class="titlepage">
<div>
<div>
<h2 class="title" style="clear: both"><a id=
"pitfalls"></a>5.&nbsp;Common programming pitfalls</h2>
</div>
</div>
</div>
<div class="qandaset">
<dl>
<dt>5.1. <a href="pitfalls.html#id2520814">The read-eval-print loop
is just sitting there after I've typed in my form. What's
happening?</a></dt>
<dt>5.2. <a href="pitfalls.html#id2520808">I want an array of foos,
but (make-array 10 :initial-element (make-foo)) causes strange
bugs. Why?</a></dt>
<dt>5.3. <a href="pitfalls.html#id2521239">Why does
(read-from-string "foobar" :start 3) return FOOBAR instead of
BAR?</a></dt>
<dt>5.4. <a href="pitfalls.html#id2521105">Why can't I apply #'AND
or #'OR?</a></dt>
<dt>5.5. <a href="pitfalls.html#id2521391">Why are my structure
contents wrong?</a></dt>
<dt>5.6. <a href="pitfalls.html#id2521399">Why isn't there a
DEEP-COPY function in the language?</a></dt>
</dl>
<table border="0" summary="Q and A Set">
<col align="left" width="1%" />
<tbody>
<tr class="question">
<td align="left" valign="top"><a id="id2520814"></a><a id=
"id2520957"></a><b>5.1.</b></td>
<td align="left" valign="top">
<p>The read-eval-print loop is just sitting there after I've typed
in my form. What's happening?</p>
</td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>One possible explanation for this behaviour is that you have
typed in a program that causes an infinite loop; for instance
<b class="userinput"><tt>(loop)</tt></b>.</p>
<p>However, the fact that you are surprised by this behaviour
suggests that this isn't the case; in which case a far more likely
explanation is that your form is not quite complete. You may have
typed a doublequote, vertical bar, "#|" comment beginning, or left
parenthesis that you never matched with another doublequote,
vertical bar, "|#", or right parenthesis, respectively. Try typing
a few right parentheses followed by Return.</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top"><a id="id2520808"></a><a id=
"id2520889"></a><b>5.2.</b></td>
<td align="left" valign="top">
<p>I want an array of foos, but</p>
<table border="0" bgcolor="#E0E0E0">
<tr>
<td>
<pre class="programlisting">
      (make-array 10 :initial-element (make-foo))
      
</pre></td>
</tr>
</table>
<p>causes strange bugs. Why?</p>
</td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>Well, the array created above contains 10 pointers to the same
foo, which will indeed cause strange bugs. The correct way to
initialize your array is probably</p>
<table border="0" bgcolor="#E0E0E0">
<tr>
<td>
<pre class="programlisting">
        (map-into (make-array 10) #'make-foo)
      
</pre></td>
</tr>
</table>
</td>
</tr>
<tr class="question">
<td align="left" valign="top"><a id="id2521239"></a><a id=
"id2521241"></a><b>5.3.</b></td>
<td align="left" valign="top">
<p>Why does (read-from-string "foobar" :start 3) return FOOBAR
instead of BAR?</p>
</td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p><tt class="function">READ-FROM-STRING</tt> is one of the rare
functions that takes both &amp;OPTIONAL and &amp;KEY arguments:</p>
<div class="literallayout">
<p><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<tt class=
"function">READ-FROM-STRING</tt>&nbsp;string&nbsp;&amp;OPTIONAL&nbsp;eof-error-p&nbsp;eof-value&nbsp;&amp;KEY&nbsp;:start&nbsp;:end&nbsp;:preserve-whitespace<br />

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
</div>
<p>When a function takes both types of arguments, all the optional
arguments must be specified explicitly before any of the keyword
arguments may be specified. In the example above, :START becomes
the value of the optional EOF-ERROR-P parameter and 3 is the value
of the optional EOF-VALUE parameter.</p>
<p>To get the desired result, you should use <span><b class=
"command">(read-from-string "foobar" t nil :start 3)</b></span>. If
you need to understand and use the optional arguments, please refer
to CLTL2 under READ-FROM-STRING, otherwise, this will behave as
desired for most purposes.</p>
<p>The other functions with this peculiarity in the COMMON-LISP
package are <tt class="function">PARSE-NAMESTRING</tt>, <tt class=
"function">WRITE-LINE</tt> and <tt class=
"function">WRITE-STRING</tt>.</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top"><a id="id2521105"></a><a id=
"id2521234"></a><b>5.4.</b></td>
<td align="left" valign="top">
<p>Why can't I apply <tt class="function">#'AND</tt> or <tt class=
"function">#'OR</tt>?</p>
</td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>Here's the simple, but not necessarily satisfying, answer:
<tt class="function">AND</tt> and <tt class="function">OR</tt> are
macros, not functions; <tt class="function">APPLY</tt> and
<tt class="function">FUNCALL</tt> can only be used to invoke
functions, not macros and special operators.</p>
<p>OK, so what's the <span class="emphasis"><em>real</em></span>
reason? The reason that <tt class="function">AND</tt> and
<tt class="function">OR</tt> are macros rather than functions is
because they implement control structure in addition to computing a
boolean value. They evaluate their subforms sequentially from
left/top to right/bottom, and stop evaluating subforms as soon as
the result can be determined (in the case of <tt class=
"function">AND</tt>, as soon as a subform returns NIL; in the case
of <tt class="function">OR</tt>, as soon as one returns non-NIL);
this is referred to as "short circuiting" in computer language
parlance. <tt class="function">APPLY</tt> and <tt class=
"function">FUNCALL</tt>, however, are ordinary functions;
therefore, their arguments are evaluated automatically, before they
are called. Thus, were <tt class="function">APPLY</tt> able to be
used with <tt class="function">#'AND</tt>, the short-circuiting
would be defeated.</p>
<p>Perhaps you don't really care about the short-circuiting, and
simply want the functional, boolean interpretation. While this may
be a reasonable interpretation of trying to apply AND or OR, it
doesn't generalize to other macros well, so there's no obvious way
to have the Lisp system "do the right thing" when trying to apply
macros. The only function associated with a macro is its expander
function; this function accepts and returns and form, so it cannot
be used to compute the value.</p>
<p>The Common Lisp functions <tt class="function">EVERY</tt> and
<tt class="function">SOME</tt> can be used to get the functionality
you intend when trying to apply <tt class="function">#'AND</tt> and
<tt class="function">#'OR</tt>. For instance, the erroneous form:
<b class="userinput"><tt>(apply #'and *list*)</tt></b> can be
translated to the correct form: <b class="userinput"><tt>(every
#'identity *list*)</tt></b>.</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top"><a id="id2521391"></a><a id=
"id2521424"></a><b>5.5.</b></td>
<td align="left" valign="top">
<p>Why are my structure contents wrong?</p>
</td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>Most probably, one of your structure slots is called `p'. The
accessor for this slot will clash with the default predicate that
defstruct defines for your structure; so if you have (defstruct bar
(p 1) (q 2)), you could easily see (make-bar) return #S(bar :p t :q
2).</p>
<p>To avoid this, use the :predicate defstruct option to eliminate
or rename the predicate function, or else use a different slot
name.</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top"><a id="id2521399"></a><a id=
"id2521389"></a><b>5.6.</b></td>
<td align="left" valign="top">
<p>Why isn't there a DEEP-COPY function in the language?</p>
</td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>Copying an arbitrary structure or object needs context to
determine what is the correct copy.</p>
<p>For instance, consider a queue data structure, most easily
implemented as a pair of pointers, one pointing to the head of the
queue and another to the tail</p>
</td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>See Kent Pitman's <a href=
"http://www.nhplace.com/kent/PS/EQUAL.html" target=
"_top">article</a> on the nature of equality.</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="navfooter">
<hr />
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left"><a accesskey="p" href=
"programming.html">Prev</a>&nbsp;</td>
<td width="20%" align="center"><a accesskey="u" href=
"index.html">Up</a></td>
<td width="40%" align="right">&nbsp;<a accesskey="n" href=
"myths.html">Next</a></td>
</tr>
<tr>
<td width="40%" align="left" valign="top">4.&nbsp;Programming
questions&nbsp;</td>
<td width="20%" align="center"><a accesskey="h" href=
"index.html">Home</a></td>
<td width="40%" align="right" valign="top">&nbsp;6.&nbsp;Debunking
myths</td>
</tr>
</table>
</div>
</body>
</html>
